<?php
global $_W, $_GPC;
$operation = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
$set = globalSetting('commission');
$this_model=m('commission');
$agentlevels = $this_model->getLevels();
if(isset($_GPC['sub'])){
	$time = time();
	$price = $_GPC['price'];
	$agentlevel = $_GPC['agentlevel'];
	$m_list = pdo_fetchall("select id,openid from " . tablename("eshop_member") . " where agentlevel='".$agentlevel."'");
	$number = count($m_list);
	if($number){
		foreach((array)$m_list as $k=>$v){
			$all_id.=$v['id'].',';
			member_gold($v['openid'],$price, 'addgold', '分销商分红');
			$set = globalSetting('shop');
			$logno = m('common')->createNO('member_log', 'logno', 'RC');
			$datas = array(
				'openid' => $v['openid'],
				'logno' => $logno,
				'uniacid' => $_W['uniacid'],
				'type' => '0',
				'createtime' => TIMESTAMP,
				'status' => '1',
				'title' => '分销商分红',
				'money' => $price,
				'rechargetype' => 'system'
			);
			pdo_insert('eshop_member_log', $datas);
		}
		$data['price'] = $price;
		$data['agentlevel'] = $agentlevel;
		$data['agentnum'] = $number;
		$data['agentallid'] = rtrim($all_id,',');
		$data['admin_id'] = $_SESSION['bjcms.comweb_account']['id'];
		$data['add_time'] = $time;
		pdo_insert('eshop_member_agent', $data);
		message('发放成功！',$this->createWebUrl('commission/divided'), 'success');
	}else{
		message("该分销商等级下人数为0");
        exit;
	}
}
$pindex    = max(1, intval($_GPC['page']));
$psize     = 20;
$params    = array();
$total = pdo_fetchcolumn("select count(id) from" . tablename('eshop_member_agent')." where 1=1");
$list  = pdo_fetchall("select *  from " . tablename('eshop_member_agent') . " where 1=1 ORDER BY id desc limit " . ($pindex - 1) * $psize . ',' . $psize, $params);
$pager = pagination($total, $pindex, $psize);
foreach((array)$list as $k=>$v){
	$list[$k]['name'] = pdo_fetchcolumn("select username from" . tablename('user')." where id=".$v['admin_id']);
	$list[$k]['agentgrade'] = pdo_fetchcolumn("select levelname from" . tablename('eshop_commission_level')." where id=".$v['agentlevel']);
}
include $this->template('divided');